package com.hiv.mapper;

import com.hiv.common.param.page.PageQueryPatientParam;
import com.hiv.mapper.domain.Patient;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * 患者Mapper接口
 *
 * @author zhanghong
 * @date 2022-09-20
 */
@Mapper
@Repository
public interface PatientMapper {
    Patient selectById(Long id);
    List<Patient> selectList(Patient patient);
    int insert(Patient patient);
    int batchInsert(List<Patient> patientList);
    int update(Patient patient);
    int deleteById(Long id);
    int deleteByIds(String[] ids);

    //@Select({"<script>",
    //        "   select count(DISTINCT antiviral_code) from patient",
    //        "   <where>",
    //        "       deleted=0",
    //        "       <if test=\"param.antiviralCode!=null and param.antiviralCode!=''\">and antiviral_code=#{param.antiviralCode}</if>",
    //        "       <if test=\"param.checkType!=null\">and check_type=#{param.checkType}</if>",
    //        "       <if test=\"param.contentRangeMin!=null\">and content&gt;#{param.contentRangeMin}</if>",
    //        "       <if test=\"param.contentRangeMax!=null\">and content&lt;#{param.contentRangeMax}</if>",
    //        "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if>",
    //        "       <if test=\"param.hospitalIdList != null and param.hospitalIdList.size() != 0\">",
    //        "           and hospital_id in",
    //        "           <foreach collection=\"param.hospitalIdList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
    //        "               #{item} ",
    //        "           </foreach> ",
    //        "       </if> ",
    //        "       <if test=\"param.samplingId!=null\">and sampling_id=#{param.samplingId}</if>",
    //        "       <if test=\"param.contentRangeMin!=null\">and content&gt;#{param.contentRangeMin}</if>",
    //        "       <if test=\"param.contentRangeMax!=null\">and content&lt;#{param.contentRangeMax}</if>",
    //        "   </where>",
    //        "</script>"})
    //long pageCount(@Param("param") PageQueryPatientParam param);
    //@Select({"<script>",
    //        "   select t.* from patient t",
    //        "   <where>",
    //        "       deleted=0",
    //        "       <if test=\"param.antiviralCode!=null and param.antiviralCode!=''\">and antiviral_code=#{param.antiviralCode}</if>",
    //        "       <if test=\"param.checkType!=null\">and check_type=#{param.checkType}</if>",
    //        "       <if test=\"param.contentRangeMin!=null\">and content&gt;#{param.contentRangeMin}</if>",
    //        "       <if test=\"param.contentRangeMax!=null\">and content&lt;#{param.contentRangeMax}</if>",
    //        "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if>",
    //        "       <if test=\"param.hospitalIdList != null and param.hospitalIdList.size() != 0\">",
    //        "           and hospital_id in",
    //        "           <foreach collection=\"param.hospitalIdList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
    //        "               #{item} ",
    //        "           </foreach> ",
    //        "       </if> ",
    //        "       <if test=\"param.samplingId!=null\">and sampling_id=#{param.samplingId}</if>",
    //        "       <if test=\"param.contentRangeMin!=null\">and content&gt;#{param.contentRangeMin}</if>",
    //        "       <if test=\"param.contentRangeMax!=null\">and content&lt;#{param.contentRangeMax}</if>",
    //        "   </where>",
    //        "   group by t.antiviral_code",
    //        "   order by create_time desc limit #{limit} offset #{offset}",
    //        "</script>"})
    //List<Patient> pageQuery(@Param("param") PageQueryPatientParam param, @Param("limit") int limit, @Param("offset") long offset);

    @Select({"<script>",
            "   select t.* from patient t",
            "   <where>",
            "       deleted=0",
            "       <if test=\"param.antiviralCode!=null and param.antiviralCode!=''\">and antiviral_code=#{param.antiviralCode}</if>",
            "       <if test=\"param.checkType!=null\">and check_type=#{param.checkType}</if>",
            "       <if test=\"param.contentRangeMin!=null\">and content&gt;#{param.contentRangeMin}</if>",
            "       <if test=\"param.contentRangeMax!=null\">and content&lt;#{param.contentRangeMax}</if>",
            "       <if test=\"param.hospitalId!=null\">and hospital_id=#{param.hospitalId}</if>",
            "       <if test=\"param.hospitalIdList != null and param.hospitalIdList.size() != 0\">",
            "           and hospital_id in",
            "           <foreach collection=\"param.hospitalIdList\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "       <if test=\"param.samplingId!=null\">and sampling_id=#{param.samplingId}</if>",
            "       <if test=\"param.contentRangeMin!=null\">and content&gt;#{param.contentRangeMin}</if>",
            "       <if test=\"param.contentRangeMax!=null\">and content&lt;#{param.contentRangeMax}</if>",
            "   </where>",
            "   order by create_time desc",
            "</script>"})
    List<Patient> listQuery(@Param("param") PageQueryPatientParam param);

    @Select({"<script>",
            "   select count(DISTINCT antiviral_code) from patient",
            "   <where>",
            "       deleted=0",
            "   </where>",
            "</script>"})
    Integer patientCountNum();

    @Select({"<script>",
            "   select * from patient",
            "   <where>",
            "       deleted=0",
            "       <if test=\"hospitalIdsSet != null and hospitalIdsSet.size() != 0\">",
            "           and hospital_id in",
            "           <foreach collection=\"hospitalIdsSet\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "</script>"})
    List<Patient> queryByHospitalIdList(@Param("hospitalIdsSet") Set<Long> hospitalIdsSet);

    @Select("select id from patient where hospital_id=#{hospitalId}")
    List<Patient> queryByHospitalId(Long hospitalId);

    @Select("select count(distinct(antiviral_code)) from patient where deleted=0 and create_time >= #{startTime} and create_time <= #{endTime}")
    Integer increasePatientCountByRange(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select({"<script>",
            "   select count(distinct(antiviral_code)) from patient",
            "   <where>",
            "       deleted=0 and create_time &gt;= #{startTime} and create_time &lt;= #{endTime}",
            "       <if test=\"hospitalIdsSet != null and hospitalIdsSet.size() != 0\">",
            "           and hospital_id in",
            "           <foreach collection=\"hospitalIdsSet\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> ",
            "               #{item} ",
            "           </foreach> ",
            "       </if> ",
            "   </where>",
            "</script>"})
    Integer increasePatientCountByRangeAndHospitalIdList(@Param("hospitalIdsSet") Set<Long> hospitalIdsSet, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
